package com.aat.dxfy.utils;

import java.util.Date;
import java.util.Properties;

import javax.mail.Address;
import javax.mail.Authenticator;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;


public class AATEmail {
	public static void main(String[] args) {
		try {
			sendCode("wuxin@umessage.com.cn","1223");
		} catch (MessagingException e) {
			e.printStackTrace();
		}
	}
	/**
	 * 
	 * @param toUser
	 * @param code
	 * @info 发送代码
	 * @throws MessagingException
	 */
	public static boolean sendCode(String toUser, String code)
			throws MessagingException {
		MailBean mail = new MailBean();
		mail.setMailServerHost("smtp.dizena.com");
		mail.setMailServerPort("25");
		mail.setValidate(true);
		mail.setUserName("postmaster@dizena.com");
		mail.setPassword("King1990");
		mail.setFromAddress("postmaster@dizena.com");
		mail.setToAddress(toUser);
		mail.setSubject("淡香雅邮箱验证服务");
		java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(
				"yyyy年MM月dd日 HH时mm分ss秒");
		mail.setContent("亲爱的用户：\n    您好，感谢您使用淡香雅服务，您的邮箱验证码是：" + code
				+ "\n请妥善保存，不要泄露！\n\n淡香雅账号服务\n"
				+ sdf.format(new java.util.Date()));
		return sendTextMail(mail);
	}

	/**
	 * 
	 * @param toUser
	 * @param content
	 * @info 发送文本内容
	 * @throws MessagingException
	 */
	public static boolean sendText(String toUser, String content)
			throws MessagingException {
		MailBean mail = new MailBean();
		mail.setMailServerHost("smtp.dizena.com");
		mail.setMailServerPort("25");
		mail.setValidate(true);
		mail.setUserName("admin@dizena.com");
		mail.setPassword("DxyAdmin123");
		mail.setFromAddress("admin@dizena.com");
		mail.setToAddress(toUser);
		mail.setSubject("淡香雅客户意见");
		java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(
				"yyyy年MM月dd日 HH时mm分ss秒");
		mail.setContent(content
				+ "\n\n--------------------------------------------\n"
				+ sdf.format(new java.util.Date()));
		return sendTextMail(mail);
	}

	/**
	 * 
	 * @param toUser
	 * @param content
	 * @info 发送HTML内容
	 */
	public static boolean sendHtml(String toUser, String url) {
		MailBean mail = new MailBean();
		mail.setMailServerHost("smtp.dizena.com");
		mail.setMailServerPort("25");
		mail.setValidate(true);
		mail.setUserName("admin@dizena.com");
		mail.setPassword("DxyAdmin123");
		mail.setFromAddress("admin@dizena.com");
		mail.setToAddress(toUser);
		mail.setSubject("淡香雅服务");
		String html="<p>亲爱的用户：</p>";
		html+="<p>";
		html+="&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 您好，欢迎您使用淡香雅服务。请点击下面的链接地址办理您需要的业务：";
		html+="</p>";
		html+="<p>";
		html+="&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <a href='"+url+"' target='_blank'>"+url+"</a>";
		html+="</p>";
		html+="<p>";
		html+="&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 感谢您的使用，祝您生活愉快！";
		html+="</p>";
		html+="<p>";
		html+="淡香雅文化科技办公室";
		html+="</p>";
		html+="<p>";
		java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(
				"yyyy年MM月dd日  HH时mm分ss秒");
		html+=sdf.format(new java.util.Date());
		html+="</p>";
		mail.setContent(html);
		return sendHtmlMail(mail);
	}

	private static boolean sendTextMail(MailBean mailBean)
			throws MessagingException {

		MailAuthenticator authenticator = null;
		Properties pro = mailBean.getProperties();
		if (mailBean.isValidate()) {

			authenticator = new MailAuthenticator(mailBean.getUserName(),
					mailBean.getPassword());
		}

		Session sendMailSession = Session
				.getDefaultInstance(pro, authenticator);
		try {

			Message mailMessage = new MimeMessage(sendMailSession);

			Address from = new InternetAddress(mailBean.getFromAddress());

			mailMessage.setFrom(from);

			Address to = new InternetAddress(mailBean.getToAddress());
			mailMessage.setRecipient(Message.RecipientType.TO, to);

			mailMessage.setSubject(mailBean.getSubject());

			mailMessage.setSentDate(new Date());

			String mailContent = mailBean.getContent();
			mailMessage.setText(mailContent);

			Transport.send(mailMessage);
			return true;
		} catch (MessagingException ex) {
			ex.printStackTrace();
		}
		return false;
	}

	/**
	 * HTML形式发送邮件
	 * 
	 * @param mailBean
	 * @return
	 */
	private static boolean sendHtmlMail(MailBean mailBean) {

		MailAuthenticator authenticator = null;
		Properties pro = mailBean.getProperties();

		if (mailBean.isValidate()) {
			authenticator = new MailAuthenticator(mailBean.getUserName(),
					mailBean.getPassword());
		}

		Session sendMailSession = Session
				.getDefaultInstance(pro, authenticator);
		try {

			Message mailMessage = new MimeMessage(sendMailSession);

			Address from = new InternetAddress(mailBean.getFromAddress());

			mailMessage.setFrom(from);

			Address to = new InternetAddress(mailBean.getToAddress());

			mailMessage.setRecipient(Message.RecipientType.TO, to);

			mailMessage.setSubject(mailBean.getSubject());

			mailMessage.setSentDate(new Date());

			Multipart mainPart = new MimeMultipart();

			BodyPart html = new MimeBodyPart();

			html.setContent(mailBean.getContent(), "text/html; charset=utf-8");
			mainPart.addBodyPart(html);

			mailMessage.setContent(mainPart);

			Transport.send(mailMessage);
			return true;
		} catch (MessagingException ex) {
			ex.printStackTrace();
		}
		return false;
	}

}

class MailAuthenticator extends Authenticator {
	String userName = null;
	String password = null;

	public MailAuthenticator() {
		super();
	}

	public MailAuthenticator(String username, String password) {
		this.userName = username;
		this.password = password;
	}

	protected PasswordAuthentication getPasswordAuthentication() {
		return new PasswordAuthentication(userName, password);
	}
}

class MailBean {
	private String mailServerHost;
	private String mailServerPort = "25";

	private String fromAddress;// 发件人邮箱

	private String toAddress;// 收件人邮箱

	private String userName;
	private String password;

	private boolean validate = false;

	private String subject;// 标题

	private String content;// 内容

	private String[] attachFileNames;// 附件名称

	public Properties getProperties() {
		Properties p = new Properties();
		p.put("mail.smtp.host", this.mailServerHost);
		p.put("mail.smtp.port", this.mailServerPort);
		p.put("mail.smtp.auth", validate ? "true" : "false");
		return p;
	}

	public String getMailServerHost() {
		return mailServerHost;
	}

	public void setMailServerHost(String mailServerHost) {
		this.mailServerHost = mailServerHost;
	}

	public String getMailServerPort() {
		return mailServerPort;
	}

	public void setMailServerPort(String mailServerPort) {
		this.mailServerPort = mailServerPort;
	}

	public String getFromAddress() {
		return fromAddress;
	}

	public void setFromAddress(String fromAddress) {
		this.fromAddress = fromAddress;
	}

	public String getToAddress() {
		return toAddress;
	}

	public void setToAddress(String toAddress) {
		this.toAddress = toAddress;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public boolean isValidate() {
		return validate;
	}

	public void setValidate(boolean validate) {
		this.validate = validate;
	}

	public String getSubject() {
		return subject;
	}

	public void setSubject(String subject) {
		this.subject = subject;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public String[] getAttachFileNames() {
		return attachFileNames;
	}

	public void setAttachFileNames(String[] attachFileNames) {
		this.attachFileNames = attachFileNames;
	}
}